package com.hy.movie.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

@Data
public class CommonResult {
    private Integer code;
    private String msg;
    private Object data;

    public CommonResult() {
    }

    public CommonResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static CommonResult SUCCESS() {

        return new CommonResult(0, "success");
    }

    public static CommonResult SUCCESS(Object data) {
    return new CommonResult(0,"success",data);



    }

    public static CommonResult FAIL() {
        return new CommonResult(1, "fail");
    }

    public static CommonResult FAIL(Object data) {
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            String json = objectMapper.writeValueAsString(data);
            return new CommonResult(1, "fail", data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult(1, "转义失败", data);
        }

    }
}
